package com.hei.service;

import com.hei.dao.BarticleMapper;
import com.hei.pojo.Barticle;
import com.hei.pojo.PageHelper;
import com.hei.pojo.dto.BarticleDto;
import com.hei.pojo.dto.ManageArticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BarticleServiceImpl implements  BarticleService {
    @Autowired
    private BarticleMapper barticleMapper;

    public PageHelper<BarticleDto> queryAllArticle(int currPage) {
        int totalCount = barticleMapper.totalCountArticle();
        PageHelper<BarticleDto> pageHelper=new PageHelper(currPage,totalCount,5);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("start",(currPage-1)*pageHelper.size);
        map.put("size",pageHelper.size);
        List<BarticleDto> barticles = barticleMapper.selectByPage(map);
        pageHelper.setLists(barticles);
        return pageHelper;
    }

    public Barticle selectArticleById(String id) {
        return barticleMapper.selectByPrimaryKey(id);
    }

    public void insertArticle(Barticle barticle) {
        barticleMapper.insert(barticle);
    }

    @Override
    public PageHelper<BarticleDto> queryArticleByLid(int currPage,String lid) {
        int totalCount = this.countByLid(lid);
        PageHelper<BarticleDto> pageHelper=new PageHelper(currPage,totalCount,5);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("start",(currPage-1)*pageHelper.size);
        map.put("size",pageHelper.size);
        map.put("lid",lid);
        List<BarticleDto> barticles = barticleMapper.selectByPage(map);
        pageHelper.setLists(barticles);
        return pageHelper;
    }

    @Override
    public PageHelper<BarticleDto> queryArticleByKid(int currPage,String kid) {
        int totalCount = this.countByKid(kid);
        PageHelper<BarticleDto> pageHelper=new PageHelper(currPage,totalCount,5);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("start",(currPage-1)*pageHelper.size);
        map.put("size",pageHelper.size);
        map.put("kid",kid);
        List<BarticleDto> barticles = barticleMapper.selectByPage(map);
        pageHelper.setLists(barticles);
        return pageHelper;
    }

    public int countByKid(String kid){
        return barticleMapper.countByKid(kid);
    }

    public int countByLid(String lid){
        return barticleMapper.countByLid(lid);
    }

    public int countPublishArticle(){
        return barticleMapper.totalCountArticle();
    }

    @Override
    public void updatePublish(String id) {
        barticleMapper.updatePublish(id);

    }

    @Override
    public void updateUnpublish(String id) {
        barticleMapper.updateUnpublish(id);
    }

    @Override
    public List<ManageArticle> queryManageArticle() {
        return barticleMapper.queryManageArticle();
    }

    @Override
    public void updateArticleById(Barticle article) {
        barticleMapper.updateByPrimaryKeyWithBLOBs(article);
    }

    @Override
    public void deleteArticle(String id) {
        barticleMapper.deleteByPrimaryKey(id);
    }

    public int countAllArticle(){
        return barticleMapper.unpublishTotalCountArticle();
    }
}
